package com.infitecs.eyas.mill;

import com.infitecs.eyas.boiler.dao.AirGasParameterDao;
import com.infitecs.eyas.boiler.dao.SodaParameterDao;
import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.model.SodaParameter;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.result.dao.MiddleResultDao;
import com.infitecs.eyas.result.model.MiddleResult;
import com.infitecs.eyas.result.service.MiddleResultService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by gao yang on 2015/6/28 0028
 */
@Controller
@RequestMapping("/mill/select")
public class MillSelectController {
    @Autowired
    private MillSelectService millSelectService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private ExpressionService expressionService;

    @Autowired
    private MiddleResultDao middleResultDao;

    @Autowired
    private AirGasParameterDao airGasParameterDao;

    @Autowired
    private SodaParameterDao sodaParameterDao;

    @Autowired
    private EfficiencyService efficiencyService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;

    @RequestMapping("")
    public String getMillPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "mill/choose";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "mill/choose";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        MillSelect millSelect = millSelectService.getMillSelect();
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("millSelect", millSelect);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "mill/choose";
    }

    @RequestMapping(value = "/saveValueOfMillSelect", method = {RequestMethod.POST})
    @ResponseBody
    public void saveValueOfMillSelect(Param param) {
        millSelectService.saveValueOfMillSelect(param);
    }

    @RequestMapping(value = "/getCalculateInit", method = {RequestMethod.POST})
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putMillSelectValue(sessionService.getCaseId());
    }

    @RequestMapping(value = "/Re-reference", method = RequestMethod.POST)
    @ResponseBody
    public MillSelect reReference() {
        AirGasParameter airGasParameter = airGasParameterDao.getAirGasParameter(sessionService.getCaseId());
        SodaParameter sodaParameter = sodaParameterDao.getSodaParameter(sessionService.getCaseId());
        Efficiency efficiency = efficiencyService.getEfficiency(sessionService.getCaseId());
        if (airGasParameter != null) {
            Param E3 = new Param("E3");
            E3.setParamValue(airGasParameter.getHearthAirLeakageRate());
            millSelectService.saveValueOfMillSelect(E3);
            Param E4 = new Param("E4");
            E4.setParamValue(airGasParameter.getHearthOverOutAirCoefficient());
            millSelectService.saveValueOfMillSelect(E4);
            Param E54 = new Param("E54");
            E54.setParamValue(airGasParameter.getOnceWindRate());
            millSelectService.saveValueOfMillSelect(E54);
            Param E67 = new Param("E67");
            E67.setParamValue(airGasParameter.getInOnceWindTemperature());
            millSelectService.saveValueOfMillSelect(E67);
            Param E87 = new Param("E87");
            E87.setParamValue(airGasParameter.getOutOnceWindTemperature());
            millSelectService.saveValueOfMillSelect(E87);
        }
        try {
            Param E5 = new Param("E5");
            E5.setParamValue(middleResultDao.getMiddleResult(new MiddleResult(sessionService.getCaseId(), "E52")));
            millSelectService.saveValueOfMillSelect(E5);
        } catch (Exception e) {
            System.out.println(e.toString());
        }

        if (sodaParameter != null) {

            Param E6 = new Param("E6");
            E6.setParamValue(efficiency.getE93());
            millSelectService.saveValueOfMillSelect(E6);
        }

        return millSelectService.getMillSelect();
    }

    @RequestMapping(value = "chooseSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse chooseSave(MillSelect millSelect) {
        try {
            millSelectService.chooseSave(millSelect);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
